package com.chookss.video;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.chookss.video.entity.VideoEntity;
import com.chookss.video.model.VideoSourceModel;
import com.chookss.view.CircleImageView;
import com.chookss.view.DrawableTextView;
import com.chookss.view.MarqueeTextView;
import com.chookss.view.ProgressBarView;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;

/**
 * 视频列表的adapter
 * */
public class LittleVideoListAdapter extends BaseVideoListAdapter<LittleVideoListAdapter.MyHolder, VideoSourceModel> {
    public static final String TAG = LittleVideoListAdapter.class.getSimpleName();
    private OnItemBtnClick mItemBtnClick;
    private OnUpdateHolder onUpdateHolder;
    public LittleVideoListAdapter(Context context) {
        super(context);
    }

    public void setOnUpdateHolder(OnUpdateHolder onUpdateHolder) {
        this.onUpdateHolder = onUpdateHolder;
    }

    @NonNull
    @Override
    public LittleVideoListAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_view_video, viewGroup, false);
        MyHolder holder = new MyHolder(inflate);
        View.OnClickListener l = v -> {
            if (mItemBtnClick != null) {
                mItemBtnClick.onVideoClick(v, holder.getTvCommentView(),holder.postion, holder.model.getVideo());
            }
        };
        holder.ivAvatar.setOnClickListener(l);
        holder.tvShare.setOnClickListener(l);
        holder.tvComment.setOnClickListener(l);
        holder.tvCollect.setOnClickListener(l);
        holder.tvZan.setOnClickListener(l);
        holder.tvSetsInfo.setOnClickListener(l);
        holder.tvOnePlay.setOnClickListener(l);
        holder.tvMmarquee.setOnClickListener(l);
        holder.tvPass.setOnClickListener(l);
        holder.tvNoPass.setOnClickListener(l);
        holder.tvEdit.setOnClickListener(l);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        VideoEntity video = list.get(position).getVideo();
        GlideUtils.loadAvatar(context, video.headPhotoThumbUrl,holder.ivAvatar);
        holder.tvUserName.setText("@"+video.employeeName);

        holder.tvComment.setText(String.valueOf(video.videoCommentCounts));
        holder.tvShare.setText(String.valueOf(video.videoShareCounts));
        if(Utils.isNull(video.videoCollectionsCode)){
            holder.tvSetsInfo.setVisibility(View.GONE);
        }else{
            int num=1;
            if(!Utils.isNull(video.videoSort)){
                num=Integer.parseInt(video.videoSort);
            }
            holder.tvSetsInfo.setVisibility(View.VISIBLE);
            holder.tvSetsInfo.setText(  "合集·"+video.videoCollectionsName);
        }

        holder.tvVideoTitle.setText(video.videoTitle);
        holder.postion = position;
        holder.model = list.get(position);
        int videoLikeCounts=Utils.changeNumber(video.videoLikeCounts);
        if(videoLikeCounts<0){
            videoLikeCounts=0;
        }
        int videoCollectCounts=Utils.changeNumber(video.videoCollectCounts);
        if(videoCollectCounts<0){
            videoCollectCounts=0;
        }
        holder.tvZan.setText(String.valueOf(videoLikeCounts));
        holder.tvCollect.setText(String.valueOf(videoCollectCounts));
        if("1".equals(video.isCollect)&&videoCollectCounts>0){
            holder.tvCollect.setSelected(true);
        }else{
            holder.tvCollect.setSelected(false);
        }
        if("1".equals(video.isLike)&&videoLikeCounts>0){
            holder.tvZan.setSelected(true);
        }else{
            holder.tvZan.setSelected(false);
        }
        if(Utils.isNull(video.catalogCodes)){
            holder.tvMmarquee.setVisibility(View.GONE);
        }else{
            holder.tvMmarquee.setVisibility(View.VISIBLE);
            holder.tvMmarquee.setTextColor(Color.parseColor("#ffffff"));
            holder.tvMmarquee.setTextSize(Utils.sp2px(context,14));
            holder.tvMmarquee.setSelected(true);
            holder.tvMmarquee.setText(video.catalogNames);

        }
        holder.tvOnePlay.setVisibility(View.GONE);
        holder.tvZan.setTag(video);
        holder.tvShare.setTag(video);
        holder.tvCollect.setTag(video);
        holder.tvShare.setTag(video);
        holder.tvMmarquee.setTag(video);
        holder.tvComment.setTag(video);
        holder.tvOnePlay.setTag(video);
        holder.tvPass.setTag(video);
        holder.tvNoPass.setTag(video);
        holder.tvEdit.setTag(video);
        if (onUpdateHolder != null) {
            onUpdateHolder.onHolderUpdate(holder, position, video);
        }
        holder.rlCircle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(holder.tvComplete.getVisibility()==View.VISIBLE){
                    MyToast.show("您已经完成此学习任务");
                }else{
                    if(holder.tvCompleteInit.getText().toString().equals("学习任务已完成")){
                        MyToast.show("完成学习任务");
                        holder.progress_bar.setVisibility(View.GONE);
                        holder.tvCompleteNum.setVisibility(View.GONE);
                        holder.tvCompleteInit.setVisibility(View.GONE);
                        holder.tvComplete.setVisibility(View.VISIBLE);
                        addTaskCount(video.videoId);
                    }else{
                        MyToast.show("还未达到学习任务规定时长");
                    }
                }

            }
        });
    }
    /**
     * 完成学习任务
     */
    private void addTaskCount(String videoId) {
        Map<String, String> map = new HashMap<>();
        map.put("videoId", videoId);
        map.put("type", "1");
        MyHttpRequest.postRequest(Urls.addTaskCount, this, SignUtils2.makeSignStr(map), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
            }
        });
    }
    public class  MyHolder extends BaseVideoListAdapter.BaseHolder {
        private ImageView thumb;
        public FrameLayout playerView;
        public ViewGroup mRootView;
        public int postion;
        public VideoSourceModel model;

        public CircleImageView ivAvatar;
        public ImageView  mPlayIconImageView;
        public RelativeLayout rlCircle;
        public ProgressBarView progress_bar;
        public LinearLayout llRight,llPass;
        public DrawableTextView tvCollect;
        public MarqueeTextView tvMmarquee;
        public TextView tvComplete,tvCompleteInit,tvCompleteNum,tvZan, tvComment, tvShare, tvUserName, tvVideoTitle, tvSetsInfo,tvOnePlay;
        public TextView tvPass, tvNoPass, tvEdit;

        MyHolder(@NonNull View itemView) {
            super(itemView);
            Log.d(TAG,"new PlayerManager");
            mRootView = itemView.findViewById(R.id.root_view);
            thumb = itemView.findViewById(R.id.img_thumb);
            playerView = itemView.findViewById(R.id.player_view);
            mPlayIconImageView = itemView.findViewById(R.id.iv_play_icon);

            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            llPass = itemView.findViewById(R.id.llPass);
            llRight = itemView.findViewById(R.id.llRight);
            tvZan = itemView.findViewById(R.id.tvZan);
            tvCollect = itemView.findViewById(R.id.tvCollect);
            tvComment = itemView.findViewById(R.id.tvComment);
            tvShare = itemView.findViewById(R.id.tvShare);
            tvUserName = itemView.findViewById(R.id.tvUserName);
            tvVideoTitle = itemView.findViewById(R.id.tvVideoTitle);
            tvSetsInfo = itemView.findViewById(R.id.tvSetsInfo);
            tvOnePlay = itemView.findViewById(R.id.tvOnePlay);
            tvMmarquee = itemView.findViewById(R.id.tvMmarquee);
            tvPass = itemView.findViewById(R.id.tvPass);
            tvNoPass = itemView.findViewById(R.id.tvNoPass);
            tvEdit = itemView.findViewById(R.id.tvEdit);
            tvComplete = itemView.findViewById(R.id.tvComplete);
            tvCompleteNum = itemView.findViewById(R.id.tvCompleteNum);
            tvCompleteInit = itemView.findViewById(R.id.tvCompleteInit);
            rlCircle = itemView.findViewById(R.id.rlCircle);
            progress_bar = itemView.findViewById(R.id.progress_bar);
        }


        @Override
        public ImageView getCoverView() {
            return thumb;
        }
        @Override
        public RelativeLayout getCompleteRelativeLayout() {
            return rlCircle;
        }
        @Override
        public TextView getCompleteTextView() {
            return tvComplete;
        }
        @Override
        public TextView getCompleteTextNumView() {
            return tvCompleteNum;
        }
        @Override
        public TextView getCompleteTextInitView() {
            return tvCompleteInit;
        }

        @Override
        public ProgressBarView getProgress_bar() {
            return progress_bar;
        }
        @Override
        public TextView getTvCommentView() {
            return tvComment;
        }
        @Override
        public ViewGroup getContainerView() {
            return mRootView;
        }

        @Override
        public ImageView getPlayIcon(){
            return mPlayIconImageView;
        }

    }
    public interface OnItemBtnClick{
        void onVideoClick(View view,View viewParent, int position, VideoEntity entity);
    }

    public void setItemBtnClick(OnItemBtnClick mItemBtnClick) {
        this.mItemBtnClick = mItemBtnClick;
    }

    public interface OnUpdateHolder {
        void onHolderUpdate(LittleVideoListAdapter.MyHolder holder, int position, VideoEntity entity);
    }

}
